<template>
  <div>
    <div class = "shopcart">
      <div
        class = "content"
        @click = 'toggleList'>
        <div class = "content-left">
          <div class = "logo-warpper">
            <div
              class = "log"
              :class = '{highlight:totalCount>0}'>
              <i
                class = "icon-shopping_cart"
                :class = '{highlight:totalCount>0}'></i>
            </div>
            <div
              class = 'num'
              v-show = 'totalCount>0'>{{totalCount}}
            </div>
          </div>
          <div class = "price">{{totalPrice}}元</div>
          <div class = "desc">另需配送费¥{{deliveryPrice}}元</div>
        </div>
        <div class = "content-right">
          <div
            @click.stop.prevent = 'pay'
            class = 'pay'
            :class = 'payClass'>{{payDesc}}
          </div>
        </div>
      </div>
      <transition name = 'fold'>
        <div
          class = 'shopcart-list'
          v-show = 'listShow'>
          <div class = 'list-header'>
            <h1 class = 'title'>购物车</h1>
            <span
              class = 'empty'
              @click = 'empty'>清空</span>
          </div>
          <div
            class = 'list-content'
            ref = 'listContent'>
            <ul>
              <li
                class = 'food'
                v-for = "food in selectFoods">
                <span class = 'name'>{{food.name}}</span>
                <div class = 'price'>
                  <span>¥{{food.price*food.count}}</span>
                </div>
                <div class = 'cartcontrol-wrapper'>
                  <cartcontrol :food = 'food'></cartcontrol>
                </div>
              </li>
            </ul>
          </div>
        </div>
      </transition>

      <transition
        v-on:beforeEnter = "beforeEnter"
        v-on:enter = "enter"
        v-on:afterEnter = "afterEnter">
        <div
          class = 'ball'
          v-show = 'ballShow'>
          <div class = 'inner inner-hook'></div>
        </div>
      </transition>

    </div>
    <transition name = 'fade'>
      <div
        @click = 'toggleList'
        class = 'list-mask'
        v-show = 'listShow'></div>
    </transition>
  </div>
</template>

<script>
import cartcontrol from '@/components/cartcontrol/cartcontrol'
import BScroll from 'better-scroll'

export default {
  components: {
    cartcontrol,
  },
  props: {
    selectFoods: {
      type: Array,
    },
    deliveryPrice: {
      type: Number,
      default: 0,
    },
    minPrice: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      ballel: Object,
      ballShow: false,
      fold: true,
    }
  },
  watch: {},
  computed: {
    listShow() {
      if (!this.totalCount) {
        this.fold = true
        return false
      }
      let show = !this.fold
      if (show) {
        this.$nextTick(() => {
          if (!this.scroll) {
            this.scroll = new BScroll(this.$refs.listContent, {
              click: true,
            })
          } else {
            this.scroll.refresh()
          }

        })
      }
      return show
    },
    totalPrice() {
      let total = 0
      this.selectFoods.forEach((food) => {
        total += food.price * food.count
      })
      return total
    },
    totalCount() {
      let totalCount = 0
      this.selectFoods.forEach((food) => {
        totalCount += food.count
      })
      return totalCount
    },
    payDesc() {
      if (this.totalPrice === 0) {
        return `¥${this.minPrice}元起送`
      } else if (this.totalPrice < this.minPrice) {
        let diff = this.minPrice - this.totalPrice
        return `还差¥${diff}元起送`
      } else {
        return '去结算'
      }
    },
    payClass() {
      if (this.totalPrice < this.minPrice) {
        return 'not-enough'
      } else {
        return 'enough'
      }
    },
  },
  methods: {
    empty() {
      this.selectFoods.forEach((food) => {
        if (food.count) {
          food.count = 0
        }
      })
    },
    toggleList() {
      if (!this.totalCount) {
        return
      }
      this.fold = !this.fold
    },
    darp(el) {
      this.ballel = el
      this.ballShow = true
    },
    pay() {

    },
    beforeEnter(el) {
      let rect = this.ballel.getBoundingClientRect()
      let x = rect.left - 32
      let y = -(window.innerHeight - rect.top - 22)

      let inner = el.getElementsByClassName('inner-hook')[0]
      el.style.display = ''
      el.style.transform = `translate3d(${x}px,0,0)`
      el.style.webkitTransform = `translate3d(${x}px,0,0)`
      inner.style.transform = `translate3d(0,${y}px,0)`
    },
    enter(el) {
      let rf = el.offsetHeight
      this.$nextTick(() => {
        let inner = el.getElementsByClassName('inner-hook')[0]
        el.style.transform = 'translate3d(0,0,0)'
        el.style.webkitTransform = 'translate3d(0,0,0)'
        inner.style.transform = 'translate3d(0,0,0)'
      })
    },
    afterEnter(el) {
      this.ballShow = false
      el.style.display = 'none'
    },

  },
  created() {
  },
  mounted() {
  },
}
</script>
<style
  lang = "stylus"
  scoped>
  .shopcart
    position: fixed
    bottom: 0
    left: 0
    right: 0
    z-index 50
    .content
      display: flex
      height: 48px
      background: #141d27
      .content-left
        flex: 1
        font-size: 0px
        .logo-warpper
          position: relative
          display: inline-block
          vertical-align: top
          width: 56px
          height: 56px
          background: #141d27
          margin-left: 12px
          margin-right: 12px
          top: -10px
          border-radius: 50%
          padding: 6px
          box-sizing: border-box
          .log
            width: 100%
            height: 100%
            border-radius: 50%
            text-align: center
            background: #2b343c
            &.highlight
              background rgb(0, 160, 220)
            .icon-shopping_cart
              font-size: 24px
              line-height: 44px
              color: #80858a
              &.highlight
                color #fff
          .num
            position absolute
            top 0
            right 0
            width 24px
            height 16px
            line-height 16px
            background red
            border-radius 16px
            font-size 9px
            font-weight 700
            text-align center
            color white
        .price
          display inline-block
          vertical-align: top
          color rgba(255, 255, 255, 0.4)
          line-height 24px
          margin-top 12px
          padding-right 12px
          border-right 1px solid rgba(255, 255, 255, 0.1)
          box-sizing border-box
          font-size 12px
          font-weight 700
        .desc
          display inline-block
          vertical-align: top
          color rgba(255, 255, 255, 0.4)
          line-height 24px
          margin-top 12px
          margin-left 12px
          padding-right 12px
          font-size 12px
          font-weight 700
      .content-right
        flex: 0 0 105px
        width: 105px
        .pay
          font-size 12px
          font-weight 700
          color white
          text-align center
          line-height 48px
          height 48px
          color rgba(255, 255, 255, 0.4)
          &.not-enough
            background #2b333b
          &.enough
            background #00b43c
            color #fff
    .ball
      position fixed
      z-index 100
      left 32px
      bottom 22px
      transition all 0.4s linear
      .inner
        width 16px
        height 16px
        background red
        border-radius 16px
        transition all 0.4s cubic-bezier(.82, -0.61, .99, .93)
    .shopcart-list
      position absolute
      z-index -1
      width 100%
      top 0
      left 0
      transition: all 0.4s linear
      transform translate3d(0, -100%, 0)
      &.fold-enter, &.fold-leave-to
        transform translate3d(0, 0, 0)
      .list-header
        height 40px
        line-height 40px
        background #f3f5f7
        border-bottom 1px solid rgba(7, 17, 27.0 .1)
        padding 0 18px
        .title
          float left
        .empty
          float right
      .list-content
        padding 0 18px
        max-height 217px
        overflow hidden
        background white
        .food
          position relative
          padding 12px 0
          box-sizing border-box
          border-1px(rgba(7, 17, 27.0 .1))
          .name
            line-height 24px
            font-size 14px
            color rgb(7, 17, 27)
          .price
            position absolute
            line-height 24px
            right 90px
            bottom: 12px
            font-size 14px
            color red
          .cartcontrol-wrapper
            position absolute
            right 0
            bottom 6px
  .list-mask
    position fixed
    z-index 40
    top 0
    left 0
    width 100%
    height 100%
    background rgba(7, 17, 27, 0.6)
    backdrop-filter blur(10px)
    &.fade-enter-to, &.fade-leave
      transition all .4s linear
      opacity 1
    &.fade-enter, &.fade-leave-to
      opacity 0
</style>
